<?php
if (!defined("ADMREAD")) exit();

class seo extends browser
{
    var $url = '';
    var $key = '';
    var $engine = array('google' => 'google.com',
                        //'rambler' => 'rambler.ru',
                        'yandex' => 'yandex.ru',
                        'yahoo' => 'yahoo.com'
                        );
    var $lang = array(
                      'ar' => 'Arabic',
                      'hy' => 'Armenian',
                      'bs' => 'Bosnian',
                      'bg' => 'Bulgarian',
                      'zh' => 'Chinese',
                      'hr' => 'Croatian',
                      'cs' => 'Czech',
                      'da' => 'Danish',
                      'nl' => 'Dutch',
                      'en' => 'English',
                      'fa' => 'Farsi',
                      'fi' => 'Finnish',
                      'fr' => 'French',
                      'de' => 'German',
                      'el' => 'Greek',
                      'he' => 'Hebrew',
                      'hi' => 'Hindi',
                      'hu' => 'Hungarian',
                      'id' => 'Indonesian',
                      'it' => 'Italian',
                      'ja' => 'Japanese',
                      'ko' => 'Korean',
                      'mk' => 'Macedonian',
                      'no' => 'Norwegian',
                      'pl' => 'Polish',
                      'pt' => 'Portuguese',
                      'ro' => 'Romanian',
                      'ru' => 'Russian',
                      'sr' => 'Serbian',
                      'sk' => 'Slovak',
                      'es' => 'Spanish',
                      'sw' => 'Swahili',
                      'sv' => 'Swedish',
                      'tl' => 'Tagalog',
                      'th' => 'Thai',
                      'tr' => 'Turkish',
                      'uk' => 'Ukrainian'
                      );
    var $country = array(
                         'AX' => 'Aland Islands',
                         'AL' => 'Albania',
                         'DZ' => 'Algeria',
                         'AS' => 'American Samoa',
                         'AD' => 'Andorra',
                         'AO' => 'Angola',
                         'AI' => 'Anguilla',
                         'AQ' => 'Antarctica',
                         'AG' => 'Antigua and Barbuda',
                         'AR' => 'Argentina',
                         'AM' => 'Armenia',
                         'AW' => 'Aruba',
                         'AP' => 'Asia/Pacific Region',
                         'AU' => 'Australia',
                         'AT' => 'Austria',
                         'AZ' => 'Azerbaijan',
                         'BS' => 'Bahamas',
                         'BH' => 'Bahrain',
                         'BD' => 'Bangladesh',
                         'BB' => 'Barbados',
                         'BY' => 'Belarus',
                         'BE' => 'Belgium',
                         'BZ' => 'Belize',
                         'BJ' => 'Benin',
                         'BM' => 'Bermuda',
                         'BT' => 'Bhutan',
                         'BO' => 'Bolivia',
                         'BA' => 'Bosnia and Herzegovina',
                         'BW' => 'Botswana',
                         'BV' => 'Bouvet Island',
                         'BR' => 'Brazil',
                         'IO' => 'British Indian Ocean Territory',
                         'BN' => 'Brunei Darussalam',
                         'BG' => 'Bulgaria',
                         'BF' => 'Burkina Faso',
                         'BI' => 'Burundi',
                         'KH' => 'Cambodia',
                         'CM' => 'Cameroon',
                         'CA' => 'Canada',
                         'CV' => 'Cape Verde',
                         'KY' => 'Cayman Islands',
                         'CF' => 'Central African Republic',
                         'TD' => 'Chad',
                         'CL' => 'Chile',
                         'CN' => 'China',
                         'CX' => 'Christmas Island',
                         'CC' => 'Cocos (Keeling) Islands',
                         'CO' => 'Colombia',
                         'KM' => 'Comoros',
                         'CD' => 'Congo',
                         'CG' => 'Congo',
                         'CK' => 'Cook Islands',
                         'CR' => 'Costa Rica',
                         'CI' => 'Cote d\'Ivoire',
                         'HR' => 'Croatia',
                         'CU' => 'Cuba',
                         'CY' => 'Cyprus',
                         'CZ' => 'Czech Republic',
                         'DK' => 'Denmark',
                         'DJ' => 'Djibouti',
                         'DM' => 'Dominica',
                         'DO' => 'Dominican Republic',
                         'EC' => 'Ecuador',
                         'EG' => 'Egypt',
                         'SV' => 'El Salvador',
                         'GQ' => 'Equatorial Guinea',
                         'ER' => 'Eritrea',
                         'EE' => 'Estonia',
                         'ET' => 'Ethiopia',
                         'EU' => 'Europe',
                         'FK' => 'Falkland Islands (Malvinas)',
                         'FO' => 'Faroe Islands',
                         'FJ' => 'Fiji',
                         'FI' => 'Finland',
                         'FR' => 'France',
                         'GF' => 'French Guiana',
                         'PF' => 'French Polynesia',
                         'TF' => 'French Southern Territories',
                         'GA' => 'Gabon',
                         'GM' => 'Gambia',
                         'GE' => 'Georgia',
                         'DE' => 'Germany',
                         'GH' => 'Ghana',
                         'GI' => 'Gibraltar',
                         'GR' => 'Greece',
                         'GL' => 'Greenland',
                         'GD' => 'Grenada',
                         'GP' => 'Guadeloupe',
                         'GU' => 'Guam',
                         'GT' => 'Guatemala',
                         'GG' => 'Guernsey',
                         'GN' => 'Guinea',
                         'GW' => 'Guinea-Bissau',
                         'GY' => 'Guyana',
                         'HT' => 'Haiti',
                         'HM' => 'Heard Island and McDonald Islands',
                         'VA' => 'Holy See (Vatican City State)',
                         'HN' => 'Honduras',
                         'HK' => 'Hong Kong',
                         'HU' => 'Hungary',
                         'IS' => 'Iceland',
                         'IN' => 'India',
                         'ID' => 'Indonesia',
                         'IR' => 'Iran Islamic Republic of',
                         'IQ' => 'Iraq',
                         'IE' => 'Ireland',
                         'IM' => 'Isle of Man',
                         'IL' => 'Israel',
                         'IT' => 'Italy',
                         'JM' => 'Jamaica',
                         'JP' => 'Japan',
                         'JE' => 'Jersey',
                         'JO' => 'Jordan',
                         'KZ' => 'Kazakhstan',
                         'KE' => 'Kenya',
                         'KI' => 'Kiribati',
                         'KP' => 'Korea Democratic People\'s Republic of',
                         'KR' => 'Korea Republic of',
                         'KW' => 'Kuwait',
                         'KG' => 'Kyrgyzstan',
                         'LA' => 'Lao People\'s Democratic Republic',
                         'LV' => 'Latvia',
                         'LB' => 'Lebanon',
                         'LS' => 'Lesotho',
                         'LR' => 'Liberia',
                         'LY' => 'Libyan Arab Jamahiriya',
                         'LI' => 'Liechtenstein',
                         'LT' => 'Lithuania',
                         'LU' => 'Luxembourg',
                         'MO' => 'Macao',
                         'MK' => 'Macedonia',
                         'MG' => 'Madagascar',
                         'MW' => 'Malawi',
                         'MY' => 'Malaysia',
                         'MV' => 'Maldives',
                         'ML' => 'Mali',
                         'MT' => 'Malta',
                         'MH' => 'Marshall Islands',
                         'MQ' => 'Martinique',
                         'MR' => 'Mauritania',
                         'MU' => 'Mauritius',
                         'YT' => 'Mayotte',
                         'MX' => 'Mexico',
                         'FM' => 'Micronesia Federated States of',
                         'MD' => 'Moldova Republic of',
                         'MC' => 'Monaco',
                         'MN' => 'Mongolia',
                         'ME' => 'Montenegro',
                         'MS' => 'Montserrat',
                         'MA' => 'Morocco',
                         'MZ' => 'Mozambique',
                         'MM' => 'Myanmar',
                         'NA' => 'Namibia',
                         'NR' => 'Nauru',
                         'NP' => 'Nepal',
                         'NL' => 'Netherlands',
                         'AN' => 'Netherlands Antilles',
                         'NC' => 'New Caledonia',
                         'NZ' => 'New Zealand',
                         'NI' => 'Nicaragua',
                         'NE' => 'Niger',
                         'NG' => 'Nigeria',
                         'NU' => 'Niue',
                         'NF' => 'Norfolk Island',
                         'MP' => 'Northern Mariana Islands',
                         'NO' => 'Norway',
                         'OM' => 'Oman',
                         'PK' => 'Pakistan',
                         'PW' => 'Palau',
                         'PS' => 'Palestinian Territory',
                         'PA' => 'Panama',
                         'PG' => 'Papua New Guinea',
                         'PY' => 'Paraguay',
                         'PE' => 'Peru',
                         'PH' => 'Philippines',
                         'PN' => 'Pitcairn',
                         'PL' => 'Poland',
                         'PT' => 'Portugal',
                         'PR' => 'Puerto Rico',
                         'QA' => 'Qatar',
                         'RE' => 'Reunion',
                         'RO' => 'Romania',
                         'RU' => 'Russian Federation',
                         'RW' => 'Rwanda',
                         'SH' => 'Saint Helena',
                         'KN' => 'Saint Kitts and Nevis',
                         'LC' => 'Saint Lucia',
                         'PM' => 'Saint Pierre and Miquelon',
                         'VC' => 'Saint Vincent and the Grenadines',
                         'WS' => 'Samoa',
                         'SM' => 'San Marino',
                         'ST' => 'Sao Tome and Principe',
                         'SA' => 'Saudi Arabia',
                         'SN' => 'Senegal',
                         'RS' => 'Serbia',
                         'SC' => 'Seychelles',
                         'SL' => 'Sierra Leone',
                         'SG' => 'Singapore',
                         'SK' => 'Slovakia',
                         'SI' => 'Slovenia',
                         'SB' => 'Solomon Islands',
                         'SO' => 'Somalia',
                         'ZA' => 'South Africa',
                         'GS' => 'South Georgia and the South Sandwich Islands',
                         'ES' => 'Spain',
                         'LK' => 'Sri Lanka',
                         'SD' => 'Sudan',
                         'SR' => 'Suriname',
                         'SJ' => 'Svalbard and Jan Mayen',
                         'SZ' => 'Swaziland',
                         'SE' => 'Sweden',
                         'CH' => 'Switzerland',
                         'SY' => 'Syrian Arab Republic',
                         'TW' => 'Taiwan',
                         'TJ' => 'Tajikistan',
                         'TZ' => 'Tanzania United Republic of',
                         'TH' => 'Thailand',
                         'TL' => 'Timor-Leste',
                         'TG' => 'Togo',
                         'TK' => 'Tokelau',
                         'TO' => 'Tonga',
                         'TT' => 'Trinidad and Tobago',
                         'TN' => 'Tunisia',
                         'TR' => 'Turkey',
                         'TM' => 'Turkmenistan',
                         'TC' => 'Turks and Caicos Islands',
                         'TV' => 'Tuvalu',
                         'UG' => 'Uganda',
                         'UA' => 'Ukraine',
                         'AE' => 'United Arab Emirates',
                         'GB' => 'United Kingdom',
                         'US' => 'United States',
                         'UM' => 'United States Minor Outlying Islands',
                         'UY' => 'Uruguay',
                         'UZ' => 'Uzbekistan',
                         'VU' => 'Vanuatu',
                         'VE' => 'Venezuela',
                         'VN' => 'Vietnam',
                         'VG' => 'Virgin Islands British',
                         'VI' => 'Virgin Islands U.S.',
                         'WF' => 'Wallis and Futuna',
                         'EH' => 'Western Sahara',
                         'YE' => 'Yemen',
                         'ZM' => 'Zambia',
                         'ZW' => 'Zimbabwe'
                         );
    function url($url)
    {
        if (empty($url)) {
        	return false;
        }
        if (!stristr($url, 'http://'))
        {
            $url = "http://".$url;
        }
    	$c = parse_url($url);
    	if(isset($c['host']))
    	{
    	    $this->url = $c['host'];
    		$this->url.= (isset($c['path']) && !empty($c['path'])) ? $c['path'] : '';
    		$this->url.= (isset($c['query']) && !empty($c['query'])) ? '?'.$c['query'] : '';
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }

    function organizer($url)
    {
        if (empty($url)) {
        	return false;
        }
        if (!stristr($url, 'http://'))
        {
            $url = "http://".$url;
        }
        $r['url'] = $url;
    	parent::browser();
    	$c = parent::content('http://bar-navig.yandex.ru/u?ver=2&show=32&url='.urlencode($url));
    	$c = empty($c['page']) ? '' :  $c['page'];
    	@preg_match('/value=\"(.*?)\"/si', $c, $ok);
    	$r['tic'] = @$ok[1];
    	if (preg_match('/title=\"(.*?)\"/si', $c, $ok)) {
    		$r['yc'] = 1;
    	} else {
    		$r['yc'] = 0;
    	}
        $c = parent::content('http://yandex.ru/yandsearch?text=&site='.urlencode($url).'&lr=187');
		$c = empty($c['page']) ? '' :  $c['page'];
    	if (preg_match('/<br>([0-9]+)\&/si', $c, $ok)) {
    		$r['yp'] = $ok[1];
    	} else {
    		$r['yp'] = 0;
    	}
        $c = parent::content('http://www.google.com/search?client=navclient-auto&ch='.$this->CheckHash($this->hashURL($url)).'&features=Rank&q=info:'.$url.'&num=100&filter=0');
		$c = empty($c['page']) ? '' :  $c['page'];
		preg_match('/Rank_([0-9]+):([0-9]+):([0-9]+)/si', $c, $ok);
		$r['pr'] = (isset($ok[3])) ? $ok[3] : 0;
    	$c = parent::content('http://search.dmoz.org/search/?q='.$url);
    	$c = empty($c['page']) ? '' :  $c['page'];
    	if (preg_match('/Open Directory Sites/si', $c, $ok)) {
    		$r['dmoz'] = 1;
    	} else {
    		$r['dmoz'] = 0;
    	}
    	$c = parent::content('http://www.google.com/search?q=site:'.$url);
    	$c = empty($c['page']) ? '' :  str_replace(',','',$c['page']);
    	if (preg_match('/About ([0-9]+)/si', $c, $ok)) {
    		$r['gp'] = $ok[1];
    	} else {
    		$r['gp'] = 0;
    	}
    	return $r;
    }

    function backlink()
    {
        $engine = '';
        $back['google'] = array('url'=>'http://www.google.com/search?q=link%3A','pattern'=>'/About ([0-9\,]+) results/si');
        $back['yahoo']  = array('url'=>'http://search.yahoo.com/search?p=links%3A','pattern'=>'/([0-9\,]+)<\/strong> results for/si');
        $back['bing']   = array('url'=>'http://www.bing.com/search?q=link%3A','pattern'=>'/of ([0-9\,]+) results/si');
        if (!empty($this->url))
        {
            parent::browser();
        	foreach($back as $k => $v){
        	    $c = parent::content($v['url'].urlencode($this->url));
				$c = empty($c['page']) ? '' :  $c['page'];
				preg_match($v['pattern'], $c, $r);
				$engine[$k] = (isset($r[1])) ? str_replace(',', '', $r[1]) : 0;
        	}
        }
        parent::close();
        return $engine;
    }

    function StrToNum($Str, $Check, $Magic)
    {
        $Int32Unit = 4294967296;
        $length = strlen($Str);
        for ($i = 0; $i < $length; $i++) {
            $Check *= $Magic;
            if ($Check >= $Int32Unit) {
                $Check = ($Check - $Int32Unit * (int) ($Check / $Int32Unit));
                $Check = ($Check < -2147483648) ? ($Check + $Int32Unit) : $Check;
            }
            $Check += ord($Str{$i});
        }
        return $Check;
    }

    function HashURL($String)
    {
        $Check1 = $this->StrToNum($String, 0x1505, 0x21);
        $Check2 = $this->StrToNum($String, 0, 0x1003F);
        $Check1 >>= 2;
        $Check1 = (($Check1 >> 4) & 0x3FFFFC0 ) | ($Check1 & 0x3F);
        $Check1 = (($Check1 >> 4) & 0x3FFC00 ) | ($Check1 & 0x3FF);
        $Check1 = (($Check1 >> 4) & 0x3C000 ) | ($Check1 & 0x3FFF);
        $T1 = (((($Check1 & 0x3C0) << 4) | ($Check1 & 0x3C)) <<2 ) | ($Check2 & 0xF0F );
        $T2 = (((($Check1 & 0xFFFFC000) << 4) | ($Check1 & 0x3C00)) << 0xA) | ($Check2 & 0xF0F0000 );
        return ($T1 | $T2);
    }

    function CheckHash($Hashnum)
    {
        $CheckByte = 0;
        $Flag = 0;
        $HashStr = sprintf('%u', $Hashnum);
        $length = strlen($HashStr);
        for ($i = $length-1; $i >= 0;  $i--) {
            $Re = $HashStr{$i};
            if (1 === ($Flag % 2)) {
                $Re += $Re;
                $Re = (int)($Re / 10) + ($Re % 10);
            }
            $CheckByte += $Re;
            $Flag ++;
        }
        $CheckByte %= 10;
        if (0 !== $CheckByte) {
            $CheckByte = 10 - $CheckByte;
            if (1 === ($Flag % 2) ) {
                if (1 === ($CheckByte % 2)) {
                    $CheckByte += 9;
                }
                $CheckByte >>= 1;
            }
        }
        return '7'.$CheckByte.$HashStr;
    }

    function rank()
    {
        $engine = '';
        if (!empty($this->url))
        {
            parent::browser();
		    $c = parent::content('http://www.google.com/search?client=navclient-auto&ch='.$this->CheckHash($this->hashURL($this->url)).'&features=Rank&q=info:'.$this->url.'&num=100&filter=0');
		    $c = empty($c['page']) ? '' :  $c['page'];
		    preg_match('/Rank_([0-9]+):([0-9]+):([0-9]+)/si', $c, $r);
		    $engine['google'] = (isset($r[3])) ? $r[3] : 0;
		    $c = parent::content('http://search.yaca.yandex.ru/yca/cy/ch/'.urlencode($this->url).'/');
		    $c = empty($c['page']) ? '' :  $c['page'];
		    preg_match('/ ([0-9]+)<\/b>/si', $c, $r);
		    $engine['yandex'] = (isset($r[1])) ? $r[1] : 0;
		    $c = parent::content('http://data.alexa.com/data?cli=10&dat=snbamz&url='.urlencode($this->url));
		    $c = empty($c['page']) ? '' :  $c['page'];
		    preg_match('/\<popularity url\="(.*?)" TEXT\="([0-9]+)"\/\>/si', $c, $r);
		    $engine['alexa'] = (isset($r[2])) ? $r[2] : 0;
		}
		parent::close();
        return $engine;
    }

    function keyword($engine,$key,$lang,$country)
    {
        $e = '';
        parent::browser();
        if ($engine == 'google') {
            $url = 'http://www.google.com/search?q='.urlencode($key).'&num=100';
            $url.= '&hl='.(isset($this->lang[$lang]) ? $lang : '');
            $url.= '&cr='.(isset($this->country[$country]) ? 'country'.$country : '');
            $c = parent::content($url);
            $c = empty($c['page']) ? '' :  $c['page'];
            if (preg_match_all('/<li.*?class=g.*?<a.*?href="(.*?)".*?>(.*?)<\/a>.*?<div.*?>(.*?)<cite>/is', $c, $r)) {
                  $e = array();
               	  $i = 0;
			      foreach($r[1] as $k => $v){
                      $e[$i]['url'] = strip_tags($v);
				      $e[$i]['title'] = strip_tags($r[2][$i]);
	                  $i++;
				  }
            }
        }

        if ($engine == 'yahoo') {
            $url = 'http://search.yahoo.com/search?p='.urlencode($key).'&n=100&fl=1&v=1';
            $url.= '&vl='.(isset($this->lang[$lang]) ? 'lang_'.$lang : '');
            $url.= '&vc='.(isset($this->country[$country]) ? $country : '');
            $c = parent::content($url);
            $c = empty($c['page']) ? '' :  $c['page'];
            if (preg_match_all('/<li.*?<h3><a.*?>(.*?)<\/a><\/h3>.*?<div.*?>(.*?)<\/div><span.*?>(.*?)<\/span>/is', $c, $r)) {
                  $e = array();
               	  $i = 0;
			      foreach($r[3] as $k => $v){
                      $e[$i]['url'] = strip_tags($v);
				      $e[$i]['title'] = strip_tags($r[1][$i]);
	                  $i++;
				  }
            }
        }

        if ($engine == 'yandex') {
            $url = 'http://yandex.ru/yandsearch?text='.urlencode($key).'&numdoc=50&lr=63';
            $c = parent::content($url);
            $c = empty($c['page']) ? '' :  $c['page'];
            if (preg_match_all('/<a class="agp" tabindex="2" href="(.*?)".*?>(.*?)<\/a>/is', $c, $r)) {
                  $e = array();
               	  $i = 0;
			      foreach($r[1] as $k => $v){
                      $e[$i]['url'] = strip_tags($v);
				      $e[$i]['title'] = strip_tags($r[2][$i]);
	                  $i++;
				  }
            }
            $url = 'http://yandex.ru/yandsearch?text='.urlencode($key).'&numdoc=50&p=1&lr=63';
            $c = parent::content($url);
            $c = empty($c['page']) ? '' :  $c['page'];
            if (preg_match_all('/<a class="agp" tabindex="2" href="(.*?)".*?>(.*?)<\/a>/is', $c, $r)) {
                  $p = 0;
                  foreach($r[1] as $k => $v){
                      $e[$i]['url'] = strip_tags($v);
				      $e[$i]['title'] = strip_tags($r[2][$p]);
	                  $i++;
	                  $p++;
				  }
            }
        }

        if ($engine == 'rambler') {
            $url = 'http://nova.rambler.ru/srch?query='.urlencode($key).'&pagelen=50';
            $c = parent::content($url);
            $c = empty($c['page']) ? '' :  $c['page'];

            if (preg_match_all('/<li><a class="title n_title_.*?" href="(.*?)".*?>(.*?)<\/a>/is', $c, $r)) {
                  $e = array();
               	  $i = 0;
			      foreach($r[1] as $k => $v){
                      $e[$i]['url'] = strip_tags($v);
				      $e[$i]['title'] = strip_tags($r[2][$i]);
	                  $i++;
				  }
            }
            $url = 'http://nova.rambler.ru/srch?query='.urlencode($key).'&pagelen=50&page=2';
            $c = parent::content($url);
            $c = empty($c['page']) ? '' :  $c['page'];
            if (preg_match_all('/<li><a class="title n_title_.*?" href="(.*?)".*?>(.*?)<\/a>/is', $c, $r)) {
                  $p = 0;
                  foreach($r[1] as $k => $v){
                      $e[$i]['url'] = strip_tags($v);
				      $e[$i]['title'] = strip_tags($r[2][$p]);
	                  $i++;
	                  $p++;
				  }
            }
        }
        parent::close();
        return $e;
    }
}
?>